<?php

namespace app\controller;

use think\Request;
use app\service\AuthService;
use Exception;

class Auth
{
    protected $authService;

    public function __construct()
    {
        $this->authService = new AuthService();
    }

    /**
     * 用户注册
     */
    public function register(Request $request)
    {
        try {
            $data = $request->only(['login', 'password', 'nickname']);

            // 验证必填字段
            if (empty($data['login']) || empty($data['password'])) {
                return error('用户名和密码不能为空');
            }

            // 验证密码长度
            if (strlen($data['password']) < 6) {
                return error('密码长度不能少于6位');
            }

            $user = $this->authService->register($data);

            return success([
                'user' => $user,
            ], '注册成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 用户登录
     */
    public function login(Request $request)
    {
        try {
            $login = $request->param('login');
            $password = $request->param('password');
            $deviceInfo = $request->param('device_info', []);

            // 验证必填字段
            if (empty($login) || empty($password)) {
                return error('用户名和密码不能为空');
            }

            $result = $this->authService->login($login, $password, $deviceInfo);

            return success($result, '登录成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 微信登录
     */
    public function wechatLogin(Request $request)
    {
        try {
            $code = $request->param('code');
            $platform = $request->param('platform', 'mini');
            $deviceInfo = $request->param('device_info', []);

            if (empty($code)) {
                return error('授权码不能为空');
            }

            $result = $this->authService->wechatLogin($code, $platform, $deviceInfo);

            return success($result, '微信登录成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 支付宝登录
     */
    public function alipayLogin(Request $request)
    {
        try {
            $authCode = $request->param('auth_code');
            $deviceInfo = $request->param('device_info', []);

            if (empty($authCode)) {
                return error('授权码不能为空');
            }

            $result = $this->authService->alipayLogin($authCode, $deviceInfo);

            return success($result, '支付宝登录成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 抖音登录
     */
    public function douyinLogin(Request $request)
    {
        try {
            $code = $request->param('code');
            $deviceInfo = $request->param('device_info', []);

            if (empty($code)) {
                return error('授权码不能为空');
            }

            $result = $this->authService->douyinLogin($code, $deviceInfo);

            return success($result, '抖音登录成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 绑定第三方账号
     */
    public function bindOauth(Request $request)
    {
        try {
            $userId = $request->userId; // 来自中间件
            $platform = $request->param('platform');
            $code = $request->param('code');

            if (empty($platform) || empty($code)) {
                return error('平台和授权码不能为空');
            }

            $oauth = $this->authService->bindOauth($userId, $platform, $code);

            return success($oauth, '绑定成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 解绑第三方账号
     */
    public function unbindOauth(Request $request)
    {
        try {
            $userId = $request->userId; // 来自中间件
            $platform = $request->param('platform');

            if (empty($platform)) {
                return error('平台不能为空');
            }

            $this->authService->unbindOauth($userId, $platform);

            return success(null, '解绑成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取绑定列表
     */
    public function getBindings(Request $request)
    {
        try {
            $userId = $request->userId; // 来自中间件

            $bindings = \app\model\UserOauth::where('user_id', $userId)
                ->field('platform,nickname,avatar,created_at')
                ->select();

            return success($bindings, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 刷新Token
     */
    public function refreshToken(Request $request)
    {
        try {
            $token = $request->param('token');

            if (empty($token)) {
                return error('Token不能为空');
            }

            $newToken = $this->authService->refreshToken($token);

            return success(['token' => $newToken], '刷新成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 退出登录
     */
    public function logout(Request $request)
    {
        try {
            $userId = $request->userId; // 来自中间件

            $this->authService->logout($userId);

            return success(null, '退出成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }
}